package hardy.etudejpa.config;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hardy.springutil.SpringBeanUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.hutool.core.collection.CollUtil;
import hardy.hibhelper.basecore.SessionHelper;
import hardy.hibhelper.basecore.SubHelper;
import hardy.hibhelper.basecore.sessionhandler.SessionHandler;
import hardy.hibhelper.basecore.sessionhandler.SessionHandlerSpboot2;
import hardy.hibhelper.basecore.sharding.ShardingRegist;
import hardy.hibhelper.basecore.sharding.SubTableInfo;
import hardy.hibhelper.basecore.sharding.TableCielAlgorithm;
import hardy.hibhelper.basecore.sharding.TableCielAlgorithm.QueryTableCielAlgorithm;
import hardy.hibhelper.basecore.support.BaseDao;

@Configuration
public class MyConfig {
	@PersistenceContext
	private EntityManager em;
	static {
		SessionHelper.showSql=true;
	}
	@Bean
	public SessionHandler sessionHandler() {
		SessionHandlerSpboot2 jpe = new SessionHandlerSpboot2(em);
		jpe.buildEntityInfos(); 
		jpe.buildEmbeddedInfos();
		return jpe;
	}
	
	@Bean  
	public BaseDao sessionHelper() throws IOException {
		SessionHelper sh = new SessionHelper(); 
		sh.setShandler(sessionHandler());
		sh.useInitialized(true); 
		return sh;
	}
	
	@Bean  
	public SubHelper subHelper() throws IOException {
		SubHelper sh = new SubHelper(sessionHelper());
		return sh;
	}
	
	 @Bean
	 public SpringBeanUtil springBeanUtil() {
		 return SpringBeanUtil.getInstance();
	 }
	 
	 @PostConstruct
	 public void test() {
		 ShardingRegist.addTableInfo("user_auth", SubTableInfo.build("phone")
				 .addSubTable("user_auth_0").addSubTable("user_auth_1")
				 .addSubTable("user_auth_2").addSubTable("user_auth_3")
				 .addSubTable("user_auth_4").addSubTable("user_auth_5")
				 .addSubTable("user_auth_6").addSubTable("user_auth_7")
				 .addSubTable("user_auth_8").addSubTable("user_auth_9")
				 .setUpdateAlgorithm(new TableCielAlgorithm.UpdateTableCielAlgorithm() {
					
					@Override
					public String getMappingTable(SubTableInfo info, Map<String, Comparable<?>> params) {
						return "user_auth_"+String.valueOf(params.get("phone")).substring(0, 1);
//						if(String.valueOf(params.get("phone")).startsWith("177")) return "user_auth_1";
//						else return "user_auth_2";
					}
				}).setQueryAlgorithm(new QueryTableCielAlgorithm() {
					
					@Override
					public List<String> getMappingTable(SubTableInfo info, Map<String, Comparable<?>> params) {
						if(params.containsKey("phone")) {
							 return CollUtil.newArrayList("user_auth_"+String.valueOf(params.get("phone")).substring(0, 1));
						}else return CollUtil.newArrayList("user_auth_1","user_auth_2","user_auth_3","user_auth_4","user_auth_5","user_auth_6","user_auth_7","user_auth_8","user_auth_9","user_auth_0");
					}
				}));
	 }
}
